package cn.itcast.nems.approval.service.impl;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.apache.tomcat.Jar;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import cn.itcast.nems.approval.dao.entity.ApprovalFormFlowInformedReadflag;
import cn.itcast.nems.approval.dao.mapper.ApprovalFormFlowInformedReadflagMapper;
import cn.itcast.nems.approval.service.ApprovalFormFlowInformedReadflagService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 流程审批单阅读状态;（限抄送的审批单） 服务实现类
 * </p>
 *
 * @author CodeGenerator
 * @since 2024-01-05
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ApprovalFormFlowInformedReadflagServiceImpl implements ApprovalFormFlowInformedReadflagService {
    
    private final ApprovalFormFlowInformedReadflagMapper mapper;
    
    @Override
    public int save(String formId, String userEmail) {
        log.debug("用户[{}]已读审批单[{}]", userEmail, formId);
        
        // 查找指定审批单所有抄送给当前用户的审批流
        List<String> unreadFlows = mapper.selectUnreadInformed(formId, userEmail);
        if (!CollectionUtils.isEmpty(unreadFlows)) {
            unreadFlows.forEach(flowId-> {
                // 将所有抄送给我审批流标记为已读
                ApprovalFormFlowInformedReadflag entity = new ApprovalFormFlowInformedReadflag();
                entity.setFormId(formId);
                entity.setFormFlowId(flowId);
                entity.setReader(userEmail);
                mapper.insert(entity);
                log.info("【保存指定审批单和用户的抄送阅读状态】持久化数据：{}" ,
                        JSON.toJSONString(entity, SerializerFeature.DisableCircularReferenceDetect));
            });
        }
        
        return CollectionUtils.isEmpty(unreadFlows) ? 0 : unreadFlows.size();
    }

}
